package com.zxiw.zxedu.recyclerview.adapter

import android.R.attr.textColor
import android.graphics.Bitmap
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.net.Uri
import android.text.TextUtils
import android.text.util.Linkify
import android.view.MotionEvent
import android.view.View
import android.widget.*
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView


/**
 * @author zhangxiaoming
 * @date 2023/8/18
 * @description
 */
class HelperRecyclerViewHolder(itemView: View, layoutId: Int) :
    BaseRecyclerViewHolder(itemView, layoutId), ViewHelper {
    override fun setText(viewId: Int, value: String) {
        val view = getView<TextView>(viewId)
        view?.text = value
    }

    override fun setTextColor(viewId: Int, color: Int) {
        val view = getView<TextView>(viewId)
        view?.setTextColor(textColor)
    }

    override fun setTextColorRes(viewId: Int, colorRes: Int) {
        val view = getView<TextView>(viewId)
        view?.setTextColor(ContextCompat.getColor(itemView.context, colorRes))
    }

    override fun setImageResource(viewId: Int, imgResId: Int) {
        val view: ImageView? = getView(viewId)
        view?.setImageResource(imgResId)
    }

    override fun setImageDrawable(viewId: Int, drawable: Drawable) {
        val view = getView<ImageView>(viewId)
        view?.setImageDrawable(drawable)
    }

    override fun setImageDrawableRes(viewId: Int, drawableRes: Int) {
        val drawable = ContextCompat.getDrawable(itemView.context, drawableRes)
        drawable?.let { setImageDrawable(viewId, it) }
    }

    override fun setImageUrl(viewId: Int, imgUrl: String) {
        val imageView = getView<ImageView>(viewId)
        //这里可以修改为自己的图片加载库
        //Ion.with(mContext).load(url).intoImageView(imageView);
        if (TextUtils.isEmpty(imgUrl)) return

        val uri: Uri = Uri.parse(imgUrl)
        imageView?.setImageURI(uri)
    }

    override fun setImageBitmap(viewId: Int, imgBitmap: Bitmap) {
        val view = getView<ImageView>(viewId)
        view?.setImageBitmap(imgBitmap)
    }

    override fun setBackgroundColor(viewId: Int, color: Int) {
        val view = getView<View>(viewId)
        view?.setBackgroundColor(color)
    }

    override fun setBackgroundColorRes(viewId: Int, colorRes: Int) {
        val view = getView<View>(viewId)
        view?.setBackgroundResource(colorRes)
    }

    override fun setVisible(viewId: Int, visible: Boolean) {
        val view = getView<View>(viewId)
        view?.visibility = if (visible) View.VISIBLE else View.GONE
    }

    override fun setTag(viewId: Int, tag: Any) {
        val view = getView<View>(viewId)
        view?.tag = tag
    }

    override fun setTag(viewId: Int, key: Int, tag: Any) {
        val view = getView<View>(viewId)
        view?.setTag(key, tag)
    }

    override fun setChecked(viewId: Int, checked: Boolean) {
        val view: Checkable? = getView(viewId)
        view?.isChecked = checked
    }

    override fun setAdapter(viewId: Int, adapter: RecyclerView.Adapter<*>) {
        val view = getView<RecyclerView>(viewId)
        view?.adapter = adapter
    }

    override fun setAlpha(viewId: Int, value: Float) {
        getView<View>(viewId)?.alpha = value
    }

    override fun linkify(viewId: Int) {
        val view = getView<TextView>(viewId)
        view?.let { Linkify.addLinks(it, Linkify.ALL) }
    }

    override fun setTypeface(viewId: Int, typeface: Typeface) {
        val view = getView<TextView>(viewId)
        view?.let {
            it.typeface = typeface
            it.paintFlags = it.paintFlags or Paint.SUBPIXEL_TEXT_FLAG
        }
    }

    override fun setTypeface(typeface: Typeface, vararg viewIds: Int) {
        for (viewId in viewIds) {
            val view = getView<TextView>(viewId)
            view?.let {
                it.typeface = typeface
                it.paintFlags = it.paintFlags or Paint.SUBPIXEL_TEXT_FLAG
            }
        }
    }

    override fun setProgress(viewId: Int, progress: Int) {
        val view = getView<ProgressBar>(viewId)
        view?.progress = progress
    }

    override fun setProgress(viewId: Int, progress: Int, max: Int) {
        val view = getView<ProgressBar>(viewId)
        view?.let {
            it.max = max
            it.progress = progress
        }
    }

    override fun setMax(viewId: Int, max: Int) {
        val view = getView<ProgressBar>(viewId)
        view?.max = max
    }

    override fun setRating(viewId: Int, rating: Float) {
        val view = getView<RatingBar>(viewId)
        view?.rating = rating
    }

    override fun setRating(viewId: Int, rating: Float, max: Int) {
        val view = getView<RatingBar>(viewId)
        view?.let {
            it.max = max
            it.rating = rating
        }
    }

    fun setOnClickListener(viewId: Int, listener: View.OnClickListener) {
        val view = getView<View>(viewId)
        view?.setOnClickListener(listener)
    }

    fun setOnClickListener(viewId: Int, listener: (view: View) -> Unit) {
        val view = getView<View>(viewId)
        view?.setOnClickListener(listener)
    }

    fun setOnTouchListener(viewId: Int, listener: View.OnTouchListener) {
        val view = getView<View>(viewId)
        view?.setOnTouchListener(listener)
    }

    fun setOnTouchListener(viewId: Int, listener: (view: View, event: MotionEvent) -> Boolean) {
        val view = getView<View>(viewId)
        view?.setOnTouchListener(listener)
    }

    fun setOnLongClickListener(viewId: Int, listener: View.OnLongClickListener) {
        val view = getView<View>(viewId)
        view?.setOnLongClickListener(listener)
    }

    fun setOnLongClickListener(viewId: Int, listener: (view: View) -> Boolean) {
        val view = getView<View>(viewId)
        view?.setOnLongClickListener(listener)
    }
}